package com.open.yujiakj.core.base;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @program: YJ-admin
 * @description:
 * @author: honglei
 * @create: 2022-03-08 14:46
 */


public class WrapperUtil {
    private static WrapperUtil instance;

    /*非线程安全单例模式*/
    public static WrapperUtil getInstance() {
        if (null == instance){
            instance=new WrapperUtil();
        }
        return instance;
    }


    public  void initQueryToWrapper(AbstractWrapper wrapper,BaseQuery baseQuery){

        if (! CollectionUtils.isEmpty(baseQuery.getQueryConditions())) {
            initQuery2Wrapper(wrapper,baseQuery.getQueryConditions());
        }

        if (! CollectionUtils.isEmpty(baseQuery.getQuerySorts())) {
            initSortWrapper(wrapper,baseQuery.getQuerySorts());
        }
    }

    /***
     * 初试化查询条件到查询包装类
     * @param wrapper
     * @param queryConditionList
     * @return
     */
    public  AbstractWrapper  initQuery2Wrapper(AbstractWrapper wrapper, List<QueryCondition> queryConditionList){


        if (! CollectionUtils.isEmpty(queryConditionList)) {
            for (QueryCondition qc: queryConditionList) {
                if (Objects.isNull(qc) ||  ! qc.isQuery()){
                    continue;
                }
                if (Objects .nonNull(qc.getOperator())){
                    getColumnName(qc,wrapper);
                }

            }
        }




        return wrapper;

    }

    /**
     *
     * 添加查询条件进 queryWrapper
     * @param qc           参数
     * @param queryWrapper
     * @return QueryWrapper
     */
    public AbstractWrapper  getColumnName(QueryCondition qc, AbstractWrapper queryWrapper) {
        //驼峰转下划线
        String columnName= StrUtil.toUnderlineCase(qc.getColumnName());

        switch (qc.getOperator()) {
            case eq: // 等于
                queryWrapper.eq(columnName, qc.getValue());
                break;
            case ne: // 不等于
                queryWrapper.ne(columnName, qc.getValue());
                break;
            case gt: // 大于
                queryWrapper.gt(columnName, qc.getValue());
                break;
            case ge: // 大于等于
                queryWrapper.ge(columnName, qc.getValue());
                break;
            case lt: // 小于
                queryWrapper.lt(columnName, qc.getValue());
                break;
            case le: // 小于等于
                queryWrapper.le(columnName, qc.getValue());
                break;
            case likeLeft: // 左模糊
                queryWrapper.likeLeft(columnName, qc.getValue());
                break;
            case likeRight: // 右模糊
                queryWrapper.likeRight(columnName, qc.getValue());
                break;
            case like: // 全模糊
                queryWrapper.like(columnName, qc.getValue());
                break;
            case notLike: //不包含
                queryWrapper.notLike(columnName, qc.getValue());
                break;
            case in: // 在范围中
                if (null !=qc.getValue() && qc.getValue() instanceof String){
                    queryWrapper.in(columnName, qc.getValue().split(","));
                }
                if ( ! CollectionUtils.isEmpty(qc.getValueData())){
                    queryWrapper.in(columnName, qc.getValue());
                }
                break;
            case notIn: // 不在范围中
                if (null !=qc.getValue() && qc.getValue() instanceof String){
                    queryWrapper.notIn(columnName, qc.getValue().split(","));
                }
                if ( ! CollectionUtils.isEmpty(qc.getValueData())){
                    queryWrapper.notIn(columnName, qc.getValue());
                }
                break;
            case isNull: // 等于空
                queryWrapper.isNull(columnName);
                break;
            case isNotNull: // 不等于空
                queryWrapper.isNotNull(columnName);
                break;
            case exists:
                queryWrapper.exists(columnName);
                break;
            case notExists:
                // 前端无这项参数预留
                queryWrapper.notExists(columnName);
                break;
        }

        return queryWrapper;
    }


    public AbstractWrapper initFuzzy2Wrapper(AbstractWrapper queryWrapper, List<QueryCondition> fuzzySearchConditions) {

        for (QueryCondition fuzzySearchCondition : fuzzySearchConditions) {
            String columnName= StrUtil.toUnderlineCase(fuzzySearchCondition.getColumnName());
            queryWrapper.or(true).like(columnName,fuzzySearchCondition.getValue());
        }
        return queryWrapper;
    }


    /**
     *
     * 添加排序条件
     * @param
     * @param queryWrapper
     * @return QueryWrapper
     */
    public AbstractWrapper  initSortWrapper(AbstractWrapper queryWrapper,List<QuerySort> sortList) {
        if (! CollectionUtils.isEmpty(sortList)){
            StringBuilder sbAsc = new StringBuilder();
            StringBuilder sbDesc = new StringBuilder();
            for (QuerySort querySort : sortList) {
                if (Objects.isNull(querySort.getAlias()) && Objects.isNull(querySort.getColumnName())){
                    continue;
                }

                String cloum=querySort.getColumnName();
                if (Objects .nonNull( querySort.getAlias())){
                    cloum=querySort.getAlias();
                }

                if (null == querySort.getSortType() || 0== querySort.getSortType()){
                    sbAsc.append(cloum).append(",");
                }else {
                    sbDesc.append(cloum).append(",");
                }
            }

            if (StringUtils.hasLength(sbAsc.toString())) {
                sbAsc.deleteCharAt(sbAsc.length() - 1);
                queryWrapper.orderByAsc(sbAsc.toString());
            }
            if (StringUtils.hasLength(sbDesc.toString())) {
                sbDesc.deleteCharAt(sbDesc.length() - 1);
                queryWrapper.orderByDesc(sbDesc.toString());
            }

        }

        return queryWrapper;
    }



}
